﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.Data;

using JoltEnvironment.Sql;

namespace ClusterServer
{
    public partial class Client
    {
        #region Fields

        /// <summary>
        /// List of characters belonging to client.
        /// </summary>
        private List<Character> Characters = new List<Character>();

        /// <summary>
        /// The current client state.
        /// </summary>
        private int ClientState = 0;

        /// <summary>
        /// The tick of the received packet.
        /// </summary>
        private uint PacketTick = 0;

        /// <summary>
        /// The current ping state.
        /// </summary>
        private int PingState = 0;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructs a new client from socket.
        /// </summary>
        /// <param name="ClientSocket">Connection socket.</param>
        public Client(Socket ClientSocket)
        {
            this.ClientSocket = ClientSocket;

            this.SessionKey = Program.SessionManager.Generate();
            this.SendSessionKey();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Loads all the clients characters into memory.
        /// </summary>
        private void LoadCharacters()
        {
            Characters.Clear();

            DataTable CharacterTable = null;

            using (SqlDatabaseClient Client = Program.DatabaseManager.GetClient())
            {
                Client.AddParameter("account_id", this.UserID);
                Client.AddParameter("cluster_id", Program.ClusterManager.ID);

                CharacterTable = Client.ReadDataTable("SELECT * FROM character_tbl WHERE account_id=@account_id AND cluster_id=@cluster_id");
            }

            if (CharacterTable != null)
            {
                foreach (DataRow Row in CharacterTable.Rows)
                {
                    int CharacterID = (int)Row["id"];
                    List<int> Items = new List<int>();

                    DataTable ItemTable = null;

                    using (SqlDatabaseClient Client = Program.DatabaseManager.GetClient())
                    {
                        Client.AddParameter("character_id", CharacterID);

                        ItemTable = Client.ReadDataTable("SELECT item_id FROM item_tbl WHERE character_id=@character_id AND item_slot >= 0x2A;");
                    }

                    if (ItemTable != null)
                    {
                        foreach (DataRow ItemRow in ItemTable.Rows)
                        {
                            Items.Add((int)ItemRow["item_id"]);
                        }
                    }

                    this.Characters.Add(new Character()
                    {
                        CharacterID = CharacterID,
                        CharacterName = (string)Row["name"],
                        CharacterSlot = (int)Row["slot"],

                        MapID = (int)Row["map_id"],

                        HairID = (int)Row["hair_id"],
                        HairColor = (int)Row["hair_color"],
                        FaceID = (int)Row["face_id"],
                        Gender = (int)Row["gender"],

                        Class = (int)Row["class"],
                        Level = (int)Row["level"],
                        Strength = (int)Row["strength"],
                        Stamina = (int)Row["stamina"],
                        Dexterity = (int)Row["dexterity"],
                        Intelligence = (int)Row["intelligence"],

                        CharacterItems = Items,
                        CharacterPosition = new Vector3()
                        {
                            PosX = (float)Row["pos_x"],
                            PosY = (float)Row["pos_y"],
                            PosZ = (float)Row["pos_z"]
                        }
                    });
                }
            }
        }

        #endregion
    }
}
